/******************** (C) COPYRIGHT 2014 SONiX *******************************
* COMPANY:			SONiX
* DATE:					2014/01
* AUTHOR:				SA1
* IC:						SN32F288
* DESCRIPTION:	
*____________________________________________________________________________
* REVISION	Date				User		Description
*
*____________________________________________________________________________
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS TIME TO MARKET.
* SONiX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL 
* DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE
* AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN 
* IN CONNECTION WITH THEIR PRODUCTS.
*****************************************************************************/

/*_____ I N C L U D E S ____________________________________________________*/
#include	"SN32F280.h"
#include	"SN32F200_Def.h"
#include "PWM.h"
#include "CT16.h"

/*_____ D E C L A R A T I O N S ____________________________________________*/

/*_____ D E F I N I T I O N S ______________________________________________*/

void Reset_All_PWM(void);
void Enable_All_PWM(void);
/*_____ M A C R O S ________________________________________________________*/


/*_____ F U N C T I O N S __________________________________________________*/
/*****************************************************************************
* Function		: SysTick_Init
* Description	: Initialization of SysTick timer
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void PWM_Init (void)
{  
  //Enable Peripheral Clock
  SN_SYS1->AHBCLKEN_b.CT16B0CLKEN = 1;
  SN_SYS1->AHBCLKEN_b.CT16B1CLKEN = 1;
  SN_SYS1->AHBCLKEN_b.CT16B2CLKEN = 1;
  SN_SYS1->AHBCLKEN_b.CT16B3CLKEN = 1;
  SN_SYS1->AHBCLKEN_b.CT16B5CLKEN = 1;
  //Set PRE
  SN_CT16B0->PRE = PWM_PRE;
  SN_CT16B1->PRE = PWM_PRE;
  SN_CT16B2->PRE = PWM_PRE;
  SN_CT16B3->PRE = PWM_PRE;
  SN_CT16B5->PRE = PWM_PRE;
  //Set CT16Bn PWM Value(0% duty cycle)
	Reset_All_PWM();
  //Set CT16B1 MR3 as Timer
  SN_CT16B3->MR0 = PWMKEY| PWM_MAX;
	SN_CT16B3->MCTRL |= PWMKEY|mskCT16_MR0IE_EN |mskCT16_MR0RST_EN;
  //Set PWM Mode2 when TC<MRn and PWMn Enable
  SN_CT16B0->PWMCTRL = PWMKEY|mskCT16_PWM3MODE_1|mskCT16_PWM2MODE_1|mskCT16_PWM1MODE_1|mskCT16_PWM0MODE_1		
                               |mskCT16_PWM3EN_EN|mskCT16_PWM2EN_EN|mskCT16_PWM1EN_EN|mskCT16_PWM0EN_EN;
                               
  SN_CT16B1->PWMCTRL = PWMKEY|mskCT16B1_PWM4MODE_1|mskCT16B1_PWM5MODE_1|mskCT16B1_PWM6MODE_1|mskCT16B1_PWM7MODE_1 
                             |mskCT16B1_PWM8MODE_1|mskCT16B1_PWM9MODE_1|mskCT16B1_PWM10MODE_1;                       
                             
  SN_CT16B1->PWMENB  = PWMKEY|mskCT16B1_PWM4EN_EN |mskCT16B1_PWM5EN_EN|mskCT16B1_PWM6EN_EN|mskCT16B1_PWM7EN_EN 
                             |mskCT16B1_PWM8EN_EN |mskCT16B1_PWM9EN_EN|mskCT16B1_PWM10EN_EN;
                                 
  SN_CT16B2->PWMCTRL = PWMKEY|mskCT16_PWM3MODE_1|mskCT16_PWM2MODE_1|mskCT16_PWM1MODE_1|mskCT16_PWM0MODE_1		
                             |mskCT16_PWM3EN_EN|mskCT16_PWM2EN_EN|mskCT16_PWM1EN_EN|mskCT16_PWM0EN_EN;
  
  SN_CT16B5->PWMCTRL = PWMKEY|0;
	
  //disenable IO
  Enable_All_PWM();

  //Set CT16B0 as the up-counting mode.
  SN_CT16B0->TMRCTRL = (mskCT16_CRST | mskCT16_CM_EDGE_UP);
  //Wait until timer reset done.
	while (SN_CT16B0->TMRCTRL & mskCT16_CRST);	
	//Let TC start counting.
	SN_CT16B0->TMRCTRL |= mskCT16_CEN_EN;

  //Set CT16B1 as the up-counting mode.
  SN_CT16B1->TMRCTRL = (mskCT16_CRST | mskCT16_CM_EDGE_UP);
  //Wait until timer reset done.
	while (SN_CT16B1->TMRCTRL & mskCT16_CRST);	
	//Let TC start counting.
	SN_CT16B1->TMRCTRL |= mskCT16_CEN_EN;

  //Set CT16B2 as the up-counting mode.
  SN_CT16B2->TMRCTRL = (mskCT16_CRST | mskCT16_CM_EDGE_UP);
  //Wait until timer reset done.
	while (SN_CT16B2->TMRCTRL & mskCT16_CRST);	
	//Let TC start counting.
	SN_CT16B2->TMRCTRL |= mskCT16_CEN_EN;

  //Set CT16B3 as the up-counting mode.
  SN_CT16B3->TMRCTRL = (mskCT16_CRST | mskCT16_CM_EDGE_UP);
  //Wait until timer reset done.
	while (SN_CT16B3->TMRCTRL & mskCT16_CRST);	
	//Let TC start counting.
	SN_CT16B3->TMRCTRL |= mskCT16_CEN_EN;
	
  //Set CT16B5 as the up-counting mode.
  SN_CT16B5->TMRCTRL = (mskCT16_CRST | mskCT16_CM_EDGE_UP);
  //Wait until timer reset done.
	while (SN_CT16B5->TMRCTRL & mskCT16_CRST);	
	//Let TC start counting.
	SN_CT16B5->TMRCTRL |= mskCT16_CEN_EN;
  
  //Enable CT16B1's NVIC interrupt.
	NVIC_ClearPendingIRQ(CT16B3_IRQn);
	NVIC_EnableIRQ(CT16B3_IRQn);
}

/*****************************************************************************
* Function		: Reset_All_PWM
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void Reset_All_PWM(void)
{	
	SN_CT16B1->MR0 = PWMKEY|0;
	SN_CT16B1->MR1 = PWMKEY|0;	
	SN_CT16B1->MR2 = PWMKEY|0;
	SN_CT16B1->MR3 = PWMKEY|0;
	SN_CT16B1->MR4 = PWMKEY|0;
	SN_CT16B1->MR5 = PWMKEY|0;
	SN_CT16B1->MR6 = PWMKEY|0;
	SN_CT16B1->MR7 = PWMKEY|0;
	SN_CT16B1->MR8 = PWMKEY|0;
	SN_CT16B1->MR9 = PWMKEY|0;
	SN_CT16B1->MR10 = PWMKEY|0;	
	
	SN_CT16B2->MR1 = PWMKEY|0;
	SN_CT16B2->MR2 = PWMKEY|0;
	SN_CT16B2->MR3 = PWMKEY|0;
	SN_CT16B2->MR0 = PWMKEY|0;

	SN_CT16B0->MR0 = PWMKEY|0;
	SN_CT16B0->MR3 = PWMKEY|0;
	SN_CT16B0->MR2 = PWMKEY|0;
	SN_CT16B0->MR1 = PWMKEY|0;

}

/*****************************************************************************
* Function		: Enable_All_PWM
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void Enable_All_PWM(void)
{
  SN_CT16B0->PWMCTRL = PWMKEY|mskCT16_PWM3MODE_1|mskCT16_PWM2MODE_1|mskCT16_PWM1MODE_1|mskCT16_PWM0MODE_1		
                               |mskCT16_PWM3EN_EN|mskCT16_PWM2EN_EN|mskCT16_PWM1EN_EN|mskCT16_PWM0EN_EN;
                               
  SN_CT16B1->PWMCTRL = PWMKEY|mskCT16B1_PWM4MODE_1|mskCT16B1_PWM5MODE_1|mskCT16B1_PWM6MODE_1|mskCT16B1_PWM7MODE_1 
                             |mskCT16B1_PWM8MODE_1|mskCT16B1_PWM9MODE_1|mskCT16B1_PWM10MODE_1|mskCT16B1_PWM0MODE_1|mskCT16B1_PWM1MODE_1;
	
  SN_CT16B2->PWMCTRL = PWMKEY|mskCT16_PWM3MODE_1|mskCT16_PWM2MODE_1|mskCT16_PWM1MODE_1|mskCT16_PWM0MODE_1		
                             |mskCT16_PWM3EN_EN|mskCT16_PWM2EN_EN|mskCT16_PWM1EN_EN|mskCT16_PWM0EN_EN;
  
  SN_CT16B5->PWMCTRL = PWMKEY|0;
	
	SN_CT16B1->PWMIOENB =  0x7FF;
	SN_CT16B1->PWMENB = 0x7FF; 
	SN_CT16B0->PWMCTRL |= (PWMKEY|(0xF<<20));
	SN_CT16B2->PWMCTRL |= (PWMKEY|(0xF<<20));
	SN_CT16B5->PWMCTRL |= (PWMKEY|(0x0<<20));
}

/*****************************************************************************
* Function		: Disable_All_PWM
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void Disable_All_PWM(void)
{
	SN_CT16B1->PWMIOENB = 0x00;
	SN_CT16B0->PWMCTRL = (PWMKEY|0x00);
	SN_CT16B2->PWMCTRL = (PWMKEY|0x00);
	SN_CT16B3->PWMCTRL = (PWMKEY|0x00);
	SN_CT16B5->PWMCTRL = (PWMKEY|0x00);
}

/*****************************************************************************
* Function		: CT16B3_Disable
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void CT16B3_Disable(void)
{
  SN_SYS1->AHBCLKEN_b.CT16B0CLKEN = 0;
  SN_SYS1->AHBCLKEN_b.CT16B1CLKEN = 0;
  SN_SYS1->AHBCLKEN_b.CT16B2CLKEN = 0;
  SN_SYS1->AHBCLKEN_b.CT16B3CLKEN = 0;
  SN_SYS1->AHBCLKEN_b.CT16B5CLKEN = 0;
}
